<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="queryList" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" layout="inline" :model="formState" :label-col="{ style: { width: '75px' } }">
        <a-form-item name="bqid" label="" class="w-36">
          <SelectArea v-model:value="formState.bqid" use-cache set-cache @select="queryList" />
        </a-form-item>
        <a-form-item name="bedCode" label="">
          <a-input v-model:value="formState.bedCode" class="w-56 ml-2" placeholder="床位号" allow-clear />
        </a-form-item>
        <a-form-item name="zjm" label="">
          <a-input v-model:value="formState.zjm" class="w-56 ml-2" placeholder="姓名/助记码/住院号" allow-clear />
        </a-form-item>
        <a-form-item name="" label="变更范围">
          <a-range-picker v-model:value="formState.dateRange" class="w-56" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="inpAdvice" class="ml-4">
          <a-radio-group v-model:value="formState.inpAdvice" @change="queryList">
            <a-radio value="">全部</a-radio>
            <a-radio value="1">长期</a-radio>
            <a-radio value="0">临时</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="absolute top-[-9999px] hidden">
        <a-button ref="printMe" v-print="{ id: 'printMe' }">打印</a-button>
        <PrintView id="printMe" :print-data="printData" />
      </div>
      <a-button class="float-right mb-2" @click="onPrint">打印</a-button>
      <BaseTable :loading="loading" :data-source="tableData" :scroll="{ x: 1800 }" fill column-code="000106">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            <span>{{ index + 1 }}</span>
          </template>
          <template v-if="column.dataIndex === 'advChgType'">
            <span v-if="record.advChgType == '0'">新增</span>
            <span v-else-if="record.advChgType == '2'">停止</span>
            <span v-else-if="record.advChgType == '1'">作废</span>
            <span v-else>-</span>
          </template>
          <template v-if="column.dataIndex === 'advExecType'">
            <span v-if="record.advExecType == '0'">临时</span>
            <span v-else-if="record.advExecType == '1'">长期</span>
            <span v-else-if="record.advExecType == '2'">出院带药</span>
            <span v-else>-</span>
          </template>
          <div v-if="column.dataIndex === 'advName'">
            <!-- <DoctorAdvice
              :row-data="record"
              :params-key="{ mc: 'advName', gg: 'adv.gg', sl: 'clinicalDose', dw: 'clinicalDoseUnit', yf: 'deliverRoute.fyff', pl: 'fqy.pl' }"
            /> -->
            <DoctorAdviceOrder
              :row-data="record"
              :params-key="{ mc: 'advName', gg: 'spc', sl: 'clinicalDose', dw: 'clinicalDoseUnit', yf: 'deliverRoute.fyff', pl: 'fqy.plid' }"
            />
          </div>
          <template v-if="column.dataIndex === 'brxxb'"><NameItem :resource-data="record.brxxb" /></template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import SelectArea from '@/components/SelectArea'
import { getSystemConfig } from '@/utils/systemConfig'
import NameItem from '../components/NameItem'
import PrintView from './PrintView'

const loading = ref(false)
const tableData = ref([])
const formRef = ref(null)
const printData = ref({})
const printMe = ref()
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  inpAdvice: ''
})
const queryList = async () => {
  loading.value = true
  const { code, data, msg } = await post('/inp/clinicalOrder/getClinicalChangeOrderList', {
    bqid: formState.value.bqid,
    eddate: formState.value.dateRange?.[1],
    inpAdvice: formState.value.inpAdvice,
    stdate: formState.value.dateRange?.[0],
    zjm: formState.value.zjm,
    bedCode: formState.value.bedCode
  })
  if (code !== 0) return message.error(msg)
  tableData.value = data
  loading.value = false
}

// 打印
const onPrint = async () => {
  const { code, msg, data } = await post('/inp/Print/printChangeOrders', {
    bqid: formState.value.bqid,
    eddate: formState.value.dateRange?.[1],
    inpAdvice: formState.value.inpAdvice,
    stdate: formState.value.dateRange?.[0],
    zjm: formState.value.zjm,
    bedCode: formState.value.bedCode
  })
  if (code !== 0) return message.error(msg)
  if (getSystemConfig('NAVYHIS_MODE') === '1') {
    printData.value = JSON.parse(data)
    setTimeout(() => {
      printMe.value.$el.click()
    }, 1000)
  } else {
    window.openExe?.rpt(data) //调用winform打印
  }
}

onMounted(() => {
  queryList()
})
</script>
<style lang="less" scoped>
:deep(.ant-table-row-expand-icon) {
  display: none;
}
</style>
